<?php

namespace app\controllers;


use app\models\OldinPoint;
use app\models\OldoutPoint;
use app\models\Oldpagerank;

use app\models\Oldsale;
use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use app\models\LoginForm;
use yii\httpclient\Client;
use app\models\Pagerank;

class SortController extends Controller
{

    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'only' => ['position','login','logout','ajaxpagerank','sale','topc','topr','in','out','ajaxcnt','cnt','round','ajaxround',
                    'pin','pout','ajaxpin','ajaxpout'],
                'rules' => [
                    [
                        'actions' => ['position','logout','ajaxpagerank','sale','topc','topr','in','out','ajaxcnt','cnt','round','ajaxround',
                            'pin','pout','ajaxpin','ajaxpout'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                    [
                        'actions' => ['login'],
                        'allow' => true,
                        'roles' => ['?'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'logout' => ['post'],
                ],
            ],
        ];
    }

    /**
     * @inheritdoc
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
        ];
    }

    /**
     * Displays homepage.
     *
     * @return string
     */
    public function actionPosition()//总排名
    {
        return $this->render('position');
    }

    public function actionSale()//销售量肖像
    {
        if($this->isAdmin())
        {
            $airline=\Yii::$app->request->get('airline');
            return $this->render('/site/index',
                ['airline'=>$airline]
            );
        }
        else
        {
            return "没有权限";
        }
    }

    public function actionTopc()//销售量Top肖像
    {
        if($this->isAdmin())
        {
            $airline=\Yii::$app->request->get('airline');
            $dayid=\Yii::$app->request->get('dayid');
            return $this->render('/site/cook_c',
                ['airline'=>$airline,
                'dayid'=>$dayid,
                    ]
            );
        }
        else
        {
            return "没有权限";
        }

    }

    public function actionTopr()//销售额Top肖像
    {
        if($this->isAdmin())
        {
            $airline=\Yii::$app->request->get('airline');
            $dayid=\Yii::$app->request->get('dayid');
            return $this->render('/site/cook_r',
                ['airline'=>$airline,
                    'dayid'=>$dayid,
                ]
            );
        }
        else
        {
            return "没有权限";
        }
    }

    public function actionIn()//入度肖像
    {
        if($this->isAdmin())
        {
            $agent=\Yii::$app->request->get('agent');
            return $this->render('in',
                ['agent'=>$agent,
                ]
            );
        }
        else
        {
            return "没有权限";
        }
    }


    public function actionOut()//出度肖像
    {
        if($this->isAdmin())
        {
            $agent=\Yii::$app->request->get('agent');
            return $this->render('out',
                ['agent'=>$agent,
                ]
            );
        }
        else
        {
            return "没有权限";
        }
    }

    /**
     * Login action.
     *
     * @return string
     */
    public function actionLogin()
    {
        if (!Yii::$app->user->isGuest) {
            return $this->goHome();
        }

        $model = new LoginForm();
        if ($model->load(Yii::$app->request->post()) && $model->login()) {
            return $this->goBack();
        }
        return $this->render('/site/login', [
            'model' => $model,
        ]);
    }

    /**
     * Logout action.
     *
     * @return string
     */
    public function actionLogout()
    {
        Yii::$app->user->logout();

        return $this->goHome();
    }


    public function actionPagerank()
    {
        return $this->render('pagerank');
    }

    public function actionCnt()
    {
        return $this->render('cnt');
    }

    public function actionRound()
    {
        return $this->render('round');
    }

    public function actionPin()
    {
        return $this->render('pin');
    }

    public function actionPout()
    {
        return $this->render('pout');
    }



    public function actionAjaxpagerank()//代理人在航空公司的销售情况
    {
        $dayid=\Yii::$app->request->post('dayid');
        $curpage=\Yii::$app->request->post('page',0);
        if($curpage<0)
            $curpage=0;
        $pageSize=20;
        $dayid=(int)$dayid;
        if($this->isAdmin())
        {
            $sql='SELECT * FROM pagerank WHERE day_id=\''.$dayid.'\' ORDER BY pagerank DESC LIMIT '.$curpage*$pageSize.','.$pageSize.';';
            $range='全市场';
            $r=Oldpagerank::find()->where(['day_id'=>$dayid])->andWhere(['>','id',0])->orderBy(['pagerank'=>SORT_DESC])->limit($pageSize)->offset($curpage*$pageSize)
                ->asArray()->all();
        }else
        {
            $role=$this->getRole();
            $airnum=substr($role,1);
            $sql='SELECT * FROM pagerank,edge WHERE edge.day_id = \''.$dayid.'\' and pagerank.day_id = \''.$dayid.'\' and edge.sale_id=\'67'.$airnum.'\' and pagerank.id=edge.buy_id ORDER BY pagerank DESC LIMIT '.$curpage*$pageSize.','.$pageSize.';';
            $r=Pagerank::findBySql($sql)->asArray()->all();
            $range=$role;
        }

        $data=[];
        $data['agents']=$r;
        $data['page']=$curpage;
        $data['range']=$range;
        $data=json_encode($data);
        return $data;
    }


    public function actionAjaxcnt()//销售量排序
    {
        $dayid = \Yii::$app->request->post('dayid');
        $dayid=(int)$dayid;
        $curPage = \Yii::$app->request->post('page',0);
        if($curPage<0)
            $curPage=0;
        $pageSize=20;
        $agents= Oldsale::find()->where(['day_id'=>$dayid])->andwhere(['like','sale_nbr','O'])->orderBy(['cnt'=>SORT_DESC])->limit($pageSize)->offset($curPage*$pageSize)->asArray()->all();
        $r=[];
        $r['page']=$curPage;
        $r['agents']=$agents;
        $r=json_encode($r);
        return $r;
    }

    public function actionAjaxround()//销售额排序
    {
        $dayid = \Yii::$app->request->post('dayid');
        $dayid=(int)$dayid;
        $curPage = \Yii::$app->request->post('page',0);
        if($curPage<0)
            $curPage=0;
        $pageSize=20;
        $agents= Oldsale::find()->where(['day_id'=>$dayid])->andwhere(['like','sale_nbr','O'])->orderBy(['round'=>SORT_DESC])->limit($pageSize)->offset($curPage*$pageSize)->asArray()->all();
        $r=[];
        $r['page']=$curPage;
        $r['agents']=$agents;
        $r=json_encode($r);
        return $r;
    }


    public function actionAjaxpin()//入度排序
    {
        $dayid = \Yii::$app->request->post('dayid');
        $dayid=(int)$dayid;
        $curPage = \Yii::$app->request->post('page',0);
        if($curPage<0)
            $curPage=0;
        $pageSize=20;
        $agents= OldinPoint::find()->where(['day_id'=>$dayid])->andwhere(['like','id','O'])->orderBy(['count'=>SORT_DESC])->limit($pageSize)->offset($curPage*$pageSize)->asArray()->all();
        $r=[];
        $r['page']=$curPage;
        $r['agents']=$agents;
        $r=json_encode($r);
        return $r;
    }

    public function actionAjaxpout()//出度排序
    {
        $dayid = \Yii::$app->request->post('dayid');
        $dayid=(int)$dayid;
        $curPage = \Yii::$app->request->post('page',0);
        if($curPage<0)
            $curPage=0;
        $pageSize=20;
        $agents= OldoutPoint::find()->where(['day_id'=>$dayid])->andwhere(['like','sale_nbr','O'])->orderBy(['count'=>SORT_DESC])->limit($pageSize)->offset($curPage*$pageSize)->asArray()->all();
        $r=[];
        $r['page']=$curPage;
        $r['agents']=$agents;
        $r=json_encode($r);
        return $r;
    }



    public function getRole()
    {
        $roles=\Yii::$app->authManager->getRolesByUser(\Yii::$app->user->id);
        $r=array_keys($roles);
        if(!isset($r[0]))
            $role='';
            else
            $role=$r[0];
        return $role;
    }

    public function isAdmin()
    {
        return \Yii::$app->authManager->checkAccess(Yii::$app->user->id, 'admin');
    }

}
